/*创建提示框*/
function showMsg(type, str) {
    $('.messages-modal').removeClass('message-info message-success message-warn message-question').addClass('message-' + type);
    $('.messages-modal .modal-footer').hide();
    if (type === 'info' || type === 'success' || type === 'warn') {
        $('.messages-modal .footer-isw').show();
    } else if (type == 'question') {
        $('.messages-modal .footer-q').show();
    }
    $('.messages-modal .modal-dialog .tip').text(str);
    $('.messages-modal .modal-dialog').css({
        "top": (document.documentElement.clientHeight - 304) / 2 + $(document).scrollTop() + 'px'
    });
    $('.messages-modal').show();
}

$(function () {
    // 添加浏览器信息
    var feedbackFormData = {browser: window.browser};
    $('#brower').html(feedbackFormData.browser);

    /**/
    $('.form-group .query-results-list').on('mouseover','li',function(){
        $('.hint').css({
            'top':$(this).offset().top+ 'px'
        });
    });

    /*模态提示框关闭事件*/
    $('.messages-modal button[data-dismiss="modal"]').on('click', function () {
        $(this).parents('.messages-modal').hide();
    });

    /*x按钮控件清除事件*/
    $('#menu-search-input-container').on('click', '.input-clean-btn', function () {
        $('#menu-search-results-list').slideUp('fast');
        $('#menu-search-input').val('');
        $('#sourceSystem').val('N/A');
        $(this).hide();
        delete feedbackFormData.menuRoute;
        delete feedbackFormData.menuCode;
        delete feedbackFormData.menuName;
        delete feedbackFormData.sourceSystem;
    });

    /*菜单搜索框输入事件*/
    $('#menu-search-input').bind('input propertychange', function () {
        delete feedbackFormData.menuRoute;
        delete feedbackFormData.menuCode;
        delete feedbackFormData.menuName;
        delete feedbackFormData.sourceSystem;

        var $menuSearchInput = $(this);
        var keyword = $menuSearchInput.val();

        if (keyword.length > 0) {
            $('#menu-search-input-container').find('.input-clean-btn').show();
            if (keyword.length >= 2) {
                $.post(MENUCORE_SERVICE_URL + 'indexMenu/queryMenus', {keyword: keyword}, function (data) {
                    if (data.status == 'SUCCESS') {
                        var $resultBody = $('#menu-search-results-body');
                        $resultBody.html(''); // 搜索前清空上次搜索结果

                        if (keyword == '首页') {
                            var $resultHtml = $('<li>首页</li>');
                            // 绑定事件
                            $resultHtml.click(function () {
                                $menuSearchInput.val('首页');
                                $('#menu-search-results-list').slideUp('fast');
                                $('#sourceSystem').html('UAP');
                                feedbackFormData.menuRoute = 'UAP-UAP';
                                feedbackFormData.menuCode = 'home';
                                feedbackFormData.menuName = '首页';
                                feedbackFormData.sourceSystem = 'UAP';
                            });
                            $resultHtml.data("item", {
                                rootFunctionName: 'UAP',
                                subFunctionName: 'UAP'
                            });
                            $resultBody.append($resultHtml);
                        } else {
                            if (!data.body || data.body.length == 0) {
                                $resultBody.html('<span class="no-result">搜索不到您需要的结果 !</span>');
                            }
                        }

                        $.each(data.body, function (index, item) {
                            //只显示前20个列表信息
                            if (index >= 20) {
                                return false;
                            }
                            var $resultHtml = $('<li>' + item.uumsFunction.functionName + '</li>');
                            // 绑定事件
                            $resultHtml.click(function () {
                                $menuSearchInput.val(item.uumsFunction.functionName);
                                $('#menu-search-results-list').slideUp('fast');
                                $('#sourceSystem').html(item.uumsFunction.sourceSystem);
                                feedbackFormData.menuRoute = item.rootFunctionName + '-' + item.subFunctionName;
                                feedbackFormData.menuCode = item.uumsFunction.functionCode;
                                feedbackFormData.menuName = item.uumsFunction.functionName;
                                feedbackFormData.sourceSystem = item.uumsFunction.sourceSystem;
                            });
                            $resultHtml.data("item", item);
                            $resultBody.append($resultHtml);  // 渲染dom
                        });
                        /** 添加悬浮 菜单层级提示 **/
                        var $hint = $(
                            "<div class='hint'>" +
                            "<p class='hint-content'>" +
                            "<span></span>－<span></span>" +
                            "</p>" +
                            "<img class='tip-bg' src='resources/img/dxb.png'>" +
                            "</div>"
                        );
                        $resultBody.find("li").hover(function () {
                            var item = $(this).data("item");
                            $hint.find("span:eq(0)").html(item.rootFunctionName);
                            $hint.find("span:eq(1)").html(item.subFunctionName);
                            $(this).append($hint);
                        }, function () {
                            $hint.remove();
                        });
                        $('#menu-search-results-list').slideDown("fast");
                    }
                });
            }
        } else if ($(this).val().length == 0) {

            $(this).parents('.query-clean-box').find('.input-clean-btn').hide();
            var resultsList = $(this).parents('.query-clean-box').find('.query-results-list');
            resultsList.find('.query-results-list-body').html('');
            $('#menu-search-input').val('');
            $('#sourceSystem').val('');
            delete feedbackFormData.menuRoute;
            delete feedbackFormData.menuCode;
            delete feedbackFormData.menuName;
            delete feedbackFormData.sourceSystem;

        }
    });

    /*提交事件*/
    $('#submit').click(function () {
        if (!feedbackFormData.menuCode || !feedbackFormData.menuRoute || !feedbackFormData.menuName || !feedbackFormData.sourceSystem) {
            showMsg('warn', '菜单不能为空！');
            return;
        }
        if (!(feedbackFormData.question = $('#question').val()) || feedbackFormData.question.length <= 15) {
            showMsg('warn', '问题描述不能为空并且大于15个字！');
            return;
        }

        //禁止提交事件
        dosubmit();

        $.ajax(LOGSTATISTICS_SERVICE_URL + 'menuFeedback/addMenuFeedback', {
            success: function (data) {
                if (data.status == 'SUCCESS') {
                    window.opener.showMsg('success', '意见反馈成功！');
                    window.opener = null;
                    window.open('', '_self');
                    window.close();
                }
            },
            type: 'POST',
            data: feedbackFormData
        });

    });
    function dosubmit(){
        var btnSubmit = document.getElementById("submit");
        btnSubmit.disabled= "disabled";
        return true;
    }

    /*重置事件*/
    $('#reset').click(function () {
        $('#menu-search-input').val('');
        $('#proposePassword').val('');
        $('#sourceSystem').val('N/A');
        $('#question').val('');
        delete feedbackFormData.proposePassword;
        delete feedbackFormData.question;
        delete feedbackFormData.menuRoute;
        delete feedbackFormData.menuCode;
        delete feedbackFormData.menuName;
        delete feedbackFormData.sourceSystem;
    });

    /*问题展开事件*/
    $('ul.ul-panel').on('click', 'li.has-answer .text-info', function () {
        if ($(this).parents('li').hasClass('active')) {
            $(this).parents('li').find('.content-box').slideUp(function () {
                $(this).parents('li').removeClass('active');
            });
        } else if ($('ul.ul-panel li.has-answer.active').length == 0) {
            $(this).parents('li').find('.content-box').slideDown();
            $(this).parents('li').addClass('active');
        } else if ($('ul.ul-panel li.has-answer.active').length != 0 && !$(this).parents('li').hasClass('active')) {
            $('ul.ul-panel li.has-answer.active').find('.content-box').slideUp(function () {
                $('ul.ul-panel li.has-answer.active').removeClass('active');
            });
            $(this).parents('li').find('.content-box').slideDown(function () {
                $(this).parents('li').addClass('active');
            });
        }
    });

    var feedbackPageData = {limit: 5}

    var feedbackItemQuestionHtml =
        '<li id="{id}">' +
        '   <p class="question-title">Q：<label class="text-info"></label><label class="label-time"><span class="glyphicon glyphicon-time table_span"></span>{proposeTime}</label></p>' +
        '</li>';

    var feedbackItemAnswerHtml =
        '<div class="content-box">' +
        '   <p class="answer-title">A：<label class="label-time"><span class="glyphicon glyphicon-time table_span"></span>{processTime}</label></p>' +
        '   <p class="answer-content">{answer}</p>' +
        '</div>';

    var feedbackItemButtonHtml =
        '<div class="btn-box">' +
        '   <button name="resolved" type="button" class="btn btn-primary">已解决</button>' +
        '   <button name="unsolved" type="button" class="btn btn-default">未解决</button>' +
        '</div>';

    var changePage = function (pageNum) {
        if (pageNum && pageNum > 0) {
            feedbackPageData.start = Math.floor((pageNum - 1) * feedbackPageData.limit);
        } else {
            feedbackPageData.start = 0;
            pageNum = 1;
        }
        var $feedbackBody = $('#feedbackBody');
        $.ajax(LOGSTATISTICS_SERVICE_URL + 'menuFeedback/getByPage', {
            success: function (data) {
                if (data.status == 'SUCCESS') {
                    $feedbackBody.empty();
                    var dataCount = parseInt(data.body.totalCount);
                    var totalPage = Math.floor((dataCount + feedbackPageData.limit - 1) / feedbackPageData.limit);
                    var prePage = pageNum > 1 ? pageNum - 1 : 1
                    var nextPage = pageNum < totalPage ? pageNum + 1 : totalPage;
                    var isFirst = pageNum <= 1;
                    var isLast = pageNum >= totalPage;
                    $.each(data.body.results, function (index, item) {
                        switch (item.status) {
                            case 'UNPROCESSED':
                                var $feedbackItem = $(
                                    feedbackItemQuestionHtml.replace('{id}', item.id)
                                        .replace('{proposeTime}', item.proposeTime)
                                );
                                $feedbackItem.find('.text-info').html(item.question + '(待解决)');
                                $feedbackBody.append($feedbackItem);
                                break;
                            case 'PROCESSED':
                                var $feedbackItem = $(
                                    feedbackItemQuestionHtml.replace('{id}', item.id)
                                        .replace('{proposeTime}', item.proposeTime)
                                );
                                $feedbackItem.find('.text-info').html(item.question + '(已处理)');
                                var $feedbackAnswer = $(
                                    feedbackItemAnswerHtml.replace('{processTime}', item.processTime)
                                        .replace('{answer}', item.answer)
                                );
                                $feedbackItem.append($feedbackAnswer)
                                $feedbackItem.addClass('has-answer');

                                var $feedbackItemButton = $(feedbackItemButtonHtml);
                                $feedbackAnswer.append($feedbackItemButton);
                                $feedbackItemButton.find('button[name=resolved]').click(function () {
                                    $.post(LOGSTATISTICS_SERVICE_URL + 'menuFeedback/updateSolved', {
                                        id: item.id,
                                        status: 'RESOLVED'
                                    }, function (data) {
                                        if (data.status = 'SUCCESS') {
                                            $feedbackItem.find('.text-info').html(item.question + '(已解决)');
                                            $feedbackItemButton.remove();
                                        }
                                    });
                                });
                                $feedbackItemButton.find('button[name=unsolved]').click(function () {
                                    $.post(LOGSTATISTICS_SERVICE_URL + 'menuFeedback/updateSolved', {
                                        id: item.id,
                                        status: 'UNSOLVED'
                                    }, function (data) {
                                        if (data.status = 'SUCCESS') {
                                            changePage(pageNum);
                                        }
                                    });
                                });
                                $feedbackBody.append($feedbackItem);
                                break;
                            case 'RESOLVED':
                                var $feedbackItem = $(
                                    feedbackItemQuestionHtml.replace('{id}', item.id)
                                        .replace('{proposeTime}', item.proposeTime)
                                );
                                $feedbackItem.find('.text-info').html(item.question + '(已解决)');
                                $feedbackItem.append(
                                    feedbackItemAnswerHtml.replace('{processTime}', item.processTime)
                                        .replace('{answer}', item.answer)
                                );
                                $feedbackItem.addClass('has-answer');
                                $feedbackBody.append($feedbackItem);
                                break;
                            case 'UNSOLVED':
                                var $feedbackItem = $(
                                    feedbackItemQuestionHtml.replace('{id}', item.id)
                                        .replace('{proposeTime}', item.proposeTime)
                                );
                                $feedbackItem.find('.text-info').html(item.question + '(未解决)');
                                $feedbackItem.append(
                                    feedbackItemAnswerHtml.replace('{processTime}', item.processTime)
                                        .replace('{answer}', item.answer)
                                );
                                $feedbackItem.addClass('has-answer');
                                $feedbackBody.append($feedbackItem);
                                break;
                        }
                    });

                    if (data.body.results && data.body.results.length > 0) {
                        $('#pagination').html('第:' + pageNum + '/' + totalPage + '页');
                        var $firstPage = $('#firstPage');
                        $firstPage.off('click');
                        if (!isFirst) {
                            $firstPage.click(function () {
                                changePage();
                            });
                        }

                        var $prePage = $('#prePage');
                        $prePage.off('click');
                        if (totalPage != 1) {
                            $prePage.click(function () {
                                changePage(prePage);
                            });
                        }

                        var $nextPage = $('#nextPage');
                        $nextPage.off('click');
                        if (totalPage != 1) {
                            $nextPage.click(function () {
                                changePage(nextPage);
                            });
                        }

                        var $lastPage = $('#lastPage');
                        $lastPage.off('click');
                        if (!isLast) {
                            $lastPage.click(function () {
                                changePage(totalPage);
                            });
                        }
                    } else {
                        $('#feedbackContainer').remove();
                    }

                }
            },
            type: 'POST',
            data: feedbackPageData
        });
    }
    changePage(1);
});
